package cn.xuewei.controller;

import cn.xuewei.mapper.CourseMapper;
import cn.xuewei.pojo.entity.User;
import cn.xuewei.pojo.vo.CrouseVO;
import cn.xuewei.pojo.vo.PaidAdminVO;
import cn.xuewei.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/v1/courses/")
public class CoureseController {
@Autowired
    private CourseMapper courseMapper;
@RequestMapping("/{bvid}")
public ResultVO select(@PathVariable String bvid){
    return ResultVO.ok(courseMapper.selectByBvid(bvid));
}

    @RequestMapping("updateboughtnum/{bvid}/{boughtnum}")
    public ResultVO updateBoughtNum(@PathVariable String bvid,@PathVariable int boughtnum){
        courseMapper.updateBoughtNum(bvid,boughtnum);
        return ResultVO.ok();
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("paidAdmin")
    public ResultVO selectAdmin(){
       List<PaidAdminVO> list =  courseMapper.selectAdmin();
       return ResultVO.ok(list);
    }

    /**
     * 修改价格
     * @param bvid
     * @param price
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("{bvid}/adminPrice/{price}")
    public ResultVO adminPrice(@PathVariable String bvid,@PathVariable BigDecimal price){
        courseMapper.updatePrice(bvid,price);
        return ResultVO.ok();
    }
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("/{bvid}/{paid}/change")
    public ResultVO change(@PathVariable String bvid,@PathVariable Integer paid){
        courseMapper.change(bvid,paid);
        return ResultVO.ok();
    }
    @RequestMapping("getcoursename/{bvid}")
    public ResultVO getCourseName(@PathVariable String bvid){

        return ResultVO.ok(courseMapper.getCourseName(bvid));
    }

    @RequestMapping("recommend/{bvid}")
    public ResultVO recommend(@PathVariable String bvid){
        List<CrouseVO> list =  courseMapper.recommend(bvid);
        return ResultVO.ok(list);
    }

}
